package main

import (
	"flag"
	"cve-db/server"
	"cve-db/config"
	"cve-db/db"
	"fmt"
	"cve-db/cmd"
	"cve-db/fetcher/nvd"
	"strconv"
)

func main() {
	flag.Parse()
	if *cmd.Help || flag.NFlag() < 1 {
		flag.Usage()
		return
	}

	DebugSQL := false
	if config.Conf.DebugSQL == "true" {
		DebugSQL = true
	}

	driver, err := db.NewDB(config.Conf.DBEngine, config.Conf.DBDsn, DebugSQL)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	// 使用-s开启httpserver
	if *cmd.Server {
		server.Start(driver, config.Conf.Bind, config.Conf.Port)
	}

	// 使用-u插入和更新CVE数据
	if *cmd.Upsert{
		year := 2002
		if config.Conf.Year != "" {
			year, err = strconv.Atoi(config.Conf.Year)
			if err !=nil {
				fmt.Println(err.Error())
				return
			}
		}
		nvd.Fetchernvd(driver, year)
	}
}
